$(function(){
	
  $("#advanced_search, #search_new, #register_form, form.user_form, form.edit_user").initSearchJobRegionData();  
  $("#advanced_search, #search_new, #register_form, form.user_form, form.edit_user").initSearchJobFunctionData();
  
  $("form.user_form, form.edit_user").toggleJobAlertSelectors();
		
});

$.fn.toggleJobAlertSelectors = function (){
  return this.each(function(){
    var checkbox = $("#user_receive_emails", this);
    var selectors = $("#job_alert_selectors", this);
    if (checkbox.is(':not(:checked)')) { selectors.hide(); }
    checkbox.click(function(){
      selectors.toggle();
    });
  });
}

// Ajax load job locations and job sectors based on job region selected
$.fn.initSearchJobFunctionData = function(){
  return this.each(function(){
    var job_sector_selector = $(".job_sector_selector", this);
    var job_functions_container = $("#job_functions", this);
    
    job_sector_selector.change(function(){
      var job_sector_id = $(this).val();
      if (job_sector_id == "") { // no job sector selected
        job_functions_container.html("");
      } else {
        $.getJSON("/load_job_function_data/" + job_sector_id, function(data){
          if(data){ 
            job_functions_container.html("");
            $.each(data, function(key, value){
              row = $("<label for='job_function_"+ value.id +"'><input id='job_function_"+ value.id +"' type='checkbox' value='"+ value.id+"' name='"+ job_sector_selector.attr("name").match(/(.*?)\[/)[1] +"[job_function_ids][]'> "+ value.name +"</label>")
              $(row).appendTo(job_functions_container);              
            });        
          }
        });
      }
    });    
  });
};

// Ajax load job locations and job sectors based on job region selected
$.fn.initSearchJobRegionData = function(){
  return this.each(function(){
    var job_region_selector = $(".job_region_selector", this);
    var job_locations_container = $("#job_locations", this);
    var job_sector_selector = $(".job_sector_selector", this);
    var job_functions_container = $("#job_functions", this);
    
    job_region_selector.change(function(){
      var job_region_id = $(this).val();
      if (job_region_id == "") { // no job region selected
        job_locations_container.html("");
        $("option", job_sector_selector).remove();
        $("<option selected='selected' value=''>" + "Select region first..." + "</option>").appendTo(job_sector_selector);
        job_functions_container.html("");
      } else {
        $.getJSON("/load_job_region_data/" + job_region_id, function(data){
          if(data){

            // job location updates
            job_locations_container.html("");
            $.each(data['job_locations'], function(key, value){
              row = $("<label for='job_location_"+ value.id +"'><input id='job_location_"+ value.id +"' type='checkbox' value='"+ value.id+"' name='"+ job_region_selector.attr("name").match(/(.*?)\[/)[1] +"[job_location_ids][]'> "+ value.name +"</label>")
              $(row).appendTo(job_locations_container);              
            });
            
            //job sector updates
            job_functions_container.html("");
            $("option", job_sector_selector).remove();
            $("<option value=''>Please select...</option>").appendTo(job_sector_selector);
            $.each(data['job_sectors'], function(key, value){
              row = "<option value=\"" + value.id + "\">" + value.name + "</option>";
              $(row).appendTo(job_sector_selector);
            });
            
          }
        });
      }
    });    
  });
};